:root {
  --bg-dark: #0f1117;
  --bg-sidebar: #161b22;
  --bg-card: #1c2128;
  --bg-input: #252b36;
  --text-primary: #e6edf3;
  --text-secondary: #8b949e;
  --accent: #2f81f7;
  --accent-hover: #58a6ff;
  --border: #30363d;
  --success: #238636;
  --warning: #d29922;
  --danger: #f85149;
  --font-sans: "Inter", -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
  
  --radius-sm: 8px;
  --radius-md: 12px;
  --radius-lg: 16px;
  
  --shadow-sm: 0 1px 2px rgba(0,0,0,0.1);
  --shadow-md: 0 4px 12px rgba(0,0,0,0.15);
  --shadow-lg: 0 8px 24px rgba(0,0,0,0.25);
}

* { box-sizing: border-box; margin: 0; padding: 0; }

body {
  background-color: var(--bg-dark);
  color: var(--text-primary);
  font-family: var(--font-sans);
  height: 100vh;
  overflow: hidden;
  line-height: 1.5;
}

/* App Layout */
.app-container {
  display: flex;
  height: 100vh;
}

/* Sidebar */
.sidebar {
  width: 260px;
  background-color: var(--bg-sidebar);
  border-right: 1px solid var(--border);
  display: flex;
  flex-direction: column;
  padding: 20px 16px;
  transition: width 0.3s ease;
}

.sidebar-header {
  display: flex;
  align-items: center;
  gap: 12px;
  margin-bottom: 32px;
  padding: 0 8px;
}

.logo-icon {
  width: 32px;
  height: 32px;
  color: var(--accent);
  filter: drop-shadow(0 0 8px rgba(47, 129, 247, 0.4));
}

.sidebar h1 {
  font-size: 1.25rem;
  font-weight: 700;
  letter-spacing: -0.5px;
  background: linear-gradient(135deg, #fff 0%, #aaa 100%);
  -webkit-background-clip: text;
  -webkit-text-fill-color: transparent;
}

.sidebar-nav {
  flex: 1;
  display: flex;
  flex-direction: column;
  gap: 6px;
}

.nav-item {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 12px 16px;
  border-radius: var(--radius-sm);
  color: var(--text-secondary);
  background: none;
  border: none;
  cursor: pointer;
  text-align: left;
  font-size: 0.95rem;
  font-weight: 500;
  transition: all 0.2s ease;
}

.nav-item:hover {
  background-color: rgba(255,255,255,0.05);
  color: var(--text-primary);
  transform: translateX(4px);
}

.nav-item.active {
  background: linear-gradient(90deg, rgba(47, 129, 247, 0.15), rgba(47, 129, 247, 0.05));
  color: var(--accent);
  border-left: 3px solid var(--accent);
}

.sidebar-footer {
  margin-top: auto;
  padding: 16px;
  border-top: 1px solid var(--border);
  display: flex;
  align-items: center;
  gap: 10px;
  font-size: 0.85rem;
  color: var(--text-secondary);
  background: rgba(0,0,0,0.2);
  border-radius: var(--radius-md);
}

.status-indicator {
  width: 8px;
  height: 8px;
  border-radius: 50%;
  background-color: var(--success);
  box-shadow: 0 0 8px rgba(35, 134, 54, 0.6);
  animation: pulse 2s infinite;
}

@keyframes pulse {
  0% { box-shadow: 0 0 0 0 rgba(35, 134, 54, 0.4); }
  70% { box-shadow: 0 0 0 6px rgba(35, 134, 54, 0); }
  100% { box-shadow: 0 0 0 0 rgba(35, 134, 54, 0); }
}

/* Main Content */
.main-content {
  flex: 1;
  position: relative;
  overflow: hidden;
  display: flex;
  flex-direction: column;
  background: radial-gradient(circle at top right, #161b22 0%, #0f1117 40%);
}

.view {
  display: none;
  flex: 1;
  flex-direction: column;
  height: 100%;
  overflow-y: auto;
  animation: fadeIn 0.4s cubic-bezier(0.16, 1, 0.3, 1);
}

.view.active {
  display: flex;
}

@keyframes fadeIn {
  from { opacity: 0; transform: translateY(10px); filter: blur(4px); }
  to { opacity: 1; transform: translateY(0); filter: blur(0); }
}

/* Chat View */
.chat-header {
  height: 70px;
  border-bottom: 1px solid var(--border);
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 0 32px;
  background-color: rgba(15, 17, 23, 0.85);
  backdrop-filter: blur(12px);
  z-index: 10;
  position: sticky;
  top: 0;
}

.mode-switch {
  background-color: var(--bg-input);
  padding: 4px;
  border-radius: var(--radius-md);
  display: flex;
  gap: 2px;
  border: 1px solid var(--border);
}

.mode-btn {
  padding: 8px 20px;
  border: none;
  background: none;
  color: var(--text-secondary);
  border-radius: var(--radius-sm);
  font-size: 0.9rem;
  font-weight: 500;
  cursor: pointer;
  transition: all 0.2s;
}

.mode-btn:hover {
  color: var(--text-primary);
}

.mode-btn.active {
  background-color: var(--bg-card);
  color: var(--text-primary);
  box-shadow: var(--shadow-sm);
  font-weight: 600;
}

.header-actions {
  display: flex;
  align-items: center;
  gap: 20px;
}

.toggle-switch {
  display: flex;
  align-items: center;
  gap: 10px;
  cursor: pointer;
  font-size: 0.9rem;
  color: var(--text-secondary);
  user-select: none;
}

.toggle-switch input { display: none; }
.slider {
  width: 40px;
  height: 22px;
  background-color: var(--bg-input);
  border-radius: 22px;
  position: relative;
  transition: 0.3s cubic-bezier(0.16, 1, 0.3, 1);
  border: 1px solid var(--border);
}
.slider::before {
  content: "";
  position: absolute;
  height: 16px;
  width: 16px;
  left: 2px;
  bottom: 2px;
  background-color: var(--text-secondary);
  border-radius: 50%;
  transition: 0.3s cubic-bezier(0.16, 1, 0.3, 1);
}
input:checked + .slider {
  background-color: var(--accent);
  border-color: var(--accent);
}
input:checked + .slider::before {
  transform: translateX(18px);
  background-color: white;
}

.icon-btn {
  background: none;
  border: none;
  color: var(--text-secondary);
  cursor: pointer;
  padding: 8px;
  border-radius: 8px;
  transition: 0.2s;
  width: 36px;
  height: 36px;
  display: flex;
  align-items: center;
  justify-content: center;
}
.icon-btn:hover {
  background-color: var(--bg-input);
  color: var(--text-primary);
  transform: scale(1.05);
}

.chat-history {
  flex: 1;
  padding: 32px;
  overflow-y: auto;
  display: flex;
  flex-direction: column;
  gap: 32px;
  scroll-behavior: smooth;
  background-image: radial-gradient(rgba(47, 129, 247, 0.03) 1px, transparent 1px);
  background-size: 40px 40px;
}

.welcome-message {
  text-align: center;
  margin-top: 12vh;
  color: var(--text-secondary);
  background: var(--bg-card);
  padding: 40px;
  border-radius: var(--radius-lg);
  border: 1px solid var(--border);
  max-width: 600px;
  align-self: center;
  box-shadow: var(--shadow-lg);
}
.welcome-message h2 { margin-bottom: 16px; color: var(--text-primary); font-size: 1.8rem; }

/* Chat Bubbles */
.message {
  display: flex;
  gap: 20px;
  max-width: 960px;
  margin: 0 auto;
  width: 100%;
  animation: slideUp 0.4s cubic-bezier(0.16, 1, 0.3, 1);
}

.message.user { flex-direction: row-reverse; }

.avatar {
  width: 40px;
  height: 40px;
  border-radius: 12px;
  display: flex;
  align-items: center;
  justify-content: center;
  font-weight: bold;
  flex-shrink: 0;
  font-size: 1.1rem;
  box-shadow: var(--shadow-md);
}
.message.user .avatar {
  background: linear-gradient(135deg, var(--accent), #1a5bb8);
  color: white;
}
.message.bot .avatar {
  background: linear-gradient(135deg, var(--success), #1a632a);
  color: white;
}

.content {
  background-color: var(--bg-card);
  padding: 16px 24px;
  border-radius: 18px;
  line-height: 1.7;
  font-size: 1rem;
  position: relative;
  max-width: 85%;
  box-shadow: var(--shadow-sm);
  border: 1px solid rgba(255,255,255,0.05);
}
.message.user .content {
  background: linear-gradient(135deg, var(--accent), #2667c5);
  color: white;
  border-bottom-right-radius: 4px;
  border: none;
  box-shadow: var(--shadow-md);
}
.message.bot .content {
  border-top-left-radius: 4px;
  background-color: #1c2128;
}

.content[data-streaming="true"]::after {
  content: "▋";
  animation: blink 1s infinite;
  color: var(--accent);
  margin-left: 2px;
}

@keyframes blink { 50% { opacity: 0; } }
@keyframes slideUp { from { opacity: 0; transform: translateY(20px); } }

/* Chat Input */
.chat-input-area {
  padding: 24px 32px;
  background: rgba(15, 17, 23, 0.9);
  backdrop-filter: blur(12px);
  border-top: 1px solid var(--border);
  display: flex;
  justify-content: center;
  gap: 16px;
  max-width: 1000px;
  margin: 0 auto;
  width: 100%;
  border-radius: var(--radius-lg) var(--radius-lg) 0 0;
}

#chat-input {
  flex: 1;
  background-color: var(--bg-input);
  border: 1px solid var(--border);
  border-radius: 16px;
  padding: 16px 20px;
  color: var(--text-primary);
  font-family: inherit;
  resize: none;
  min-height: 56px;
  max-height: 200px;
  outline: none;
  transition: all 0.2s;
  font-size: 1rem;
  box-shadow: inset 0 2px 4px rgba(0,0,0,0.2);
}
#chat-input:focus {
  border-color: var(--accent);
  box-shadow: 0 0 0 3px rgba(47, 129, 247, 0.2);
  background-color: #2d333b;
}

#send-btn {
  background: linear-gradient(135deg, var(--accent), #2667c5);
  color: white;
  border: none;
  width: 56px;
  height: 56px;
  border-radius: 16px;
  cursor: pointer;
  transition: all 0.2s;
  display: flex;
  align-items: center;
  justify-content: center;
  box-shadow: var(--shadow-md);
}
#send-btn:hover {
  transform: translateY(-2px) scale(1.05);
  box-shadow: 0 6px 16px rgba(47, 129, 247, 0.4);
}
#send-btn:active { transform: translateY(0) scale(0.95); }
#send-btn:disabled {
  background: var(--bg-input);
  color: var(--text-secondary);
  cursor: not-allowed;
  box-shadow: none;
  transform: none;
}

/* Sources */
.sources-list {
  margin-top: 16px;
  padding-top: 16px;
  border-top: 1px solid rgba(255,255,255,0.1);
  font-size: 0.9rem;
}
.source-item {
  background-color: rgba(0,0,0,0.3);
  padding: 12px;
  border-radius: 8px;
  margin-bottom: 8px;
  border-left: 3px solid var(--accent);
}
.source-meta {
  display: flex;
  justify-content: space-between;
  color: var(--text-secondary);
  margin-bottom: 6px;
  font-size: 0.8rem;
  font-weight: 500;
}

/* Config View */
#view-config { padding: 48px; max-width: 1100px; margin: 0 auto; }

.view-header { margin-bottom: 48px; text-align: center; }
.view-header h2 { font-size: 2.5rem; margin-bottom: 12px; font-weight: 800; letter-spacing: -1px; }
.view-header p { color: var(--text-secondary); font-size: 1.1rem; }

.config-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(340px, 1fr));
  gap: 32px;
  padding-bottom: 100px;
}

.config-item {
  background-color: var(--bg-card);
  padding: 24px;
  border-radius: var(--radius-md);
  border: 1px solid var(--border);
  transition: all 0.2s;
  box-shadow: var(--shadow-sm);
}
.config-item:hover {
  transform: translateY(-4px);
  box-shadow: var(--shadow-lg);
  border-color: var(--accent);
}
.config-item:focus-within { border-color: var(--accent); }

.config-item label {
  display: block;
  margin-bottom: 10px;
  font-weight: 600;
  color: var(--text-secondary);
  font-size: 0.95rem;
}

.config-item input, .config-item select {
  width: 100%;
  background-color: var(--bg-dark);
  border: 1px solid var(--border);
  padding: 12px;
  border-radius: 8px;
  color: var(--text-primary);
  outline: none;
  transition: 0.2s;
  font-size: 1rem;
}
.config-item input:focus, .config-item select:focus {
  border-color: var(--accent);
  box-shadow: 0 0 0 2px rgba(47, 129, 247, 0.2);
}
.config-item .helper-text {
  margin-top: 8px;
  color: var(--text-secondary);
  font-size: 0.88rem;
  line-height: 1.4;
}

.sticky-bottom {
  position: fixed;
  bottom: 40px;
  left: 50%;
  transform: translateX(130px);
  background-color: rgba(30,30,30,0.85);
  padding: 16px 40px;
  border-radius: 50px;
  backdrop-filter: blur(16px);
  box-shadow: var(--shadow-lg);
  border: 1px solid rgba(255,255,255,0.1);
  z-index: 100;
}

.primary-btn {
  background: linear-gradient(135deg, var(--accent), #2667c5);
  color: white;
  border: none;
  padding: 12px 36px;
  border-radius: 24px;
  font-weight: 600;
  cursor: pointer;
  transition: 0.2s;
  box-shadow: var(--shadow-md);
  font-size: 1rem;
}
.primary-btn:hover {
  transform: translateY(-2px);
  box-shadow: 0 8px 20px rgba(47, 129, 247, 0.4);
}
.primary-btn:active { transform: translateY(0); }

/* Files & KB View */
#view-files { padding: 40px; max-width: 1280px; margin: 0 auto; gap: 24px; }

.kb-toolbar {
  display: flex;
  justify-content: space-between;
  gap: 20px;
  flex-wrap: wrap;
  background: var(--bg-card);
  padding: 20px;
  border-radius: var(--radius-md);
  border: 1px solid var(--border);
}
.kb-create {
  display: flex;
  gap: 12px;
  align-items: center;
}
.kb-create input {
  background: var(--bg-input);
  border: 1px solid var(--border);
  color: var(--text-primary);
  padding: 10px 16px;
  border-radius: 8px;
  min-width: 260px;
}
.kb-selector {
  display: flex;
  align-items: center;
  gap: 12px;
}
.kb-selector select {
  background: var(--bg-input);
  color: var(--text-primary);
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 10px 16px;
}

.kb-panels {
  display: grid;
  grid-template-columns: 360px 1fr;
  gap: 24px;
  align-items: start;
}
.kb-list {
  display: flex;
  flex-direction: column;
  gap: 16px;
  max-height: 75vh;
  overflow: auto;
  padding-right: 8px;
}
.kb-card {
  background: var(--bg-card);
  border: 1px solid var(--border);
  border-radius: 16px;
  padding: 20px;
  display: flex;
  flex-direction: column;
  gap: 12px;
  transition: all 0.2s;
  position: relative;
  overflow: hidden;
}
.kb-card:hover {
  transform: translateY(-2px);
  box-shadow: var(--shadow-md);
  border-color: var(--text-secondary);
}
.kb-card.active {
  border-color: var(--accent);
  background: linear-gradient(to bottom right, var(--bg-card), rgba(47, 129, 247, 0.05));
  box-shadow: 0 0 0 2px rgba(47, 129, 247, 0.3);
}
.kb-card-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
}
.kb-name { font-weight: 700; font-size: 1.1rem; }
.kb-id { color: var(--text-secondary); font-size: 0.85rem; margin-top: 2px; font-family: monospace; }
.kb-badge {
  padding: 6px 12px;
  border-radius: 999px;
  font-size: 0.8rem;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.5px;
}
.kb-badge.ok { background: rgba(35,134,54,0.2); color: var(--success); border: 1px solid rgba(35,134,54,0.3); }
.kb-badge.warn { background: rgba(210,153,34,0.15); color: var(--warning); border: 1px solid rgba(210,153,34,0.3); }
.kb-meta { display: flex; gap: 16px; color: var(--text-secondary); font-size: 0.9rem; }
.kb-paths {
  color: var(--text-secondary);
  font-size: 0.8rem;
  line-height: 1.5;
  background: rgba(0,0,0,0.2);
  padding: 8px;
  border-radius: 6px;
  font-family: monospace;
}
.kb-actions { display: flex; gap: 10px; margin-top: 4px; }

.kb-files-panel {
  background: var(--bg-card);
  border: 1px solid var(--border);
  border-radius: 16px;
  padding: 24px;
  display: flex;
  flex-direction: column;
  gap: 20px;
  min-height: 500px;
}

.upload-area {
  border: 2px dashed var(--border);
  border-radius: 16px;
  padding: 40px;
  cursor: pointer;
  transition: 0.3s;
  text-align: center;
  background: rgba(0,0,0,0.1);
}
.upload-area:hover, .upload-area.dragover {
  border-color: var(--accent);
  background-color: rgba(47, 129, 247, 0.08);
  transform: scale(1.01);
}
.upload-area svg { color: var(--text-secondary); margin-bottom: 16px; width: 56px; height: 56px; }
.upload-area h3 { margin-bottom: 8px; font-size: 1.2rem; }

.file-actions { display: flex; gap: 12px; flex-wrap: wrap; }

.action-btn {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 10px 20px;
  border-radius: 10px;
  border: 1px solid transparent;
  cursor: pointer;
  font-weight: 600;
  transition: 0.2s;
  background: rgba(255,255,255,0.08);
  color: var(--text-primary);
  font-size: 0.95rem;
}
.action-btn:hover { background: rgba(255,255,255,0.15); transform: translateY(-1px); }
.action-btn.warning { background-color: rgba(210, 153, 34, 0.15); color: var(--warning); border-color: rgba(210, 153, 34, 0.3); }
.action-btn.warning:hover { background-color: rgba(210, 153, 34, 0.25); }
.action-btn.danger { background: rgba(248,81,73,0.15); color: var(--danger); border-color: rgba(248,81,73,0.4); }
.action-btn.danger:hover { background: rgba(248,81,73,0.25); }
.action-btn.small, .primary-btn.small { padding: 8px 16px; font-size: 0.9rem; border-radius: 8px; }

.file-list {
  display: flex;
  flex-direction: column;
  gap: 10px;
}
.file-row {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 16px 20px;
  border: 1px solid var(--border);
  border-radius: 12px;
  background: var(--bg-dark);
  transition: all 0.2s;
}
.file-row:hover {
  border-color: var(--text-secondary);
  background: #13161d;
  transform: translateX(4px);
}
.file-main { display: flex; gap: 16px; align-items: center; }
.file-icon { font-size: 1.5rem; }
.file-name { font-weight: 600; font-size: 1.05rem; margin-bottom: 4px; }
.file-meta { color: var(--text-secondary); font-size: 0.85rem; }
.file-path {
  color: var(--text-secondary);
  font-size: 0.85rem;
  max-width: 300px;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  font-family: monospace;
  background: rgba(255,255,255,0.05);
  padding: 4px 8px;
  border-radius: 4px;
}
.file-empty {
  color: var(--text-secondary);
  padding: 40px;
  text-align: center;
  border: 2px dashed var(--border);
  border-radius: 12px;
}

/* Markdown Styles */
.markdown-body pre {
  background-color: #111 !important;
  padding: 16px;
  border-radius: 10px;
  overflow-x: auto;
  margin: 16px 0;
  border: 1px solid var(--border);
}
.markdown-body code {
  background-color: rgba(255,255,255,0.1);
  padding: 2px 6px;
  border-radius: 4px;
  font-family: "JetBrains Mono", monospace;
  font-size: 0.9em;
}
.markdown-body p { margin-bottom: 16px; }
.markdown-body ul, .markdown-body ol { padding-left: 24px; margin-bottom: 16px; }

/* Scrollbar */
::-webkit-scrollbar { width: 8px; }
::-webkit-scrollbar-track { background: transparent; }
::-webkit-scrollbar-thumb { background: #3b434e; border-radius: 4px; }
::-webkit-scrollbar-thumb:hover { background: #4b5563; }

/* Toast */
#toast-container {
  position: fixed;
  top: 24px;
  right: 24px;
  display: flex;
  flex-direction: column;
  gap: 12px;
  z-index: 9999;
}
.toast {
  min-width: 280px;
  max-width: 400px;
  padding: 16px 20px;
  border-radius: 12px;
  color: white;
  display: flex;
  align-items: center;
  gap: 12px;
  box-shadow: 0 12px 36px rgba(0,0,0,0.5);
  animation: toastSlideIn 0.3s cubic-bezier(0.16, 1, 0.3, 1), fadeOut 0.3s ease 3s forwards;
  backdrop-filter: blur(12px);
  font-weight: 500;
  border: 1px solid rgba(255,255,255,0.1);
}
@keyframes toastSlideIn {
  from { opacity: 0; transform: translateX(100%); }
  to { opacity: 1; transform: translateX(0); }
}
.toast.success { background: rgba(35,134,54,0.95); }
.toast.danger { background: rgba(248,81,73,0.95); }
.toast.info { background: rgba(47,129,247,0.95); }
.toast.warn { background: rgba(210,153,34,0.95); }

@keyframes fadeOut { to { opacity: 0; transform: translateY(-10px); } }
